package gowcage.slidingmenu.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import gowcage.recyclerview.R;
import gowcage.slidingmenu.presenter.BytePresenter;

/**
 * Created by Administrator on 2017/4/14.
 * 官方侧滑控件
 */

public class DrawerLayoutActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener, IDrawerLayout {

    //常量
    private final String TAG = "DrawerLayoutActivity-->";
    //控件
    private DrawerLayout mDrawerLayout;
    private ListView listview;
    private ViewFlipper flipper;
    private EditText et_raw;
    private TextView tv_convert;
    private Button btn_b2s, btn_s2b;
    //类
    private BytePresenter bytePresenter;
    private ListViewAdapter adapter;
    //数据
    private String[] strData = {"byte数组转换", "test page"};
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_drawerlayout);

        initView();
        initEvents();
        initPresenter();
    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl);

        listview = (ListView) findViewById(R.id.dl_menu_listview);
        initData();
        adapter = new ListViewAdapter(this, list);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);

        flipper = (ViewFlipper) findViewById(R.id.dl_flipper);

        initByteView();
    }

    private void initByteView() {
        View view = findViewById(R.id.dl_include_byte);
        et_raw = (EditText) view.findViewById(R.id.byte_et_rawdata);
        tv_convert = (TextView) view.findViewById(R.id.byte_tv_convertdata);
        btn_b2s = (Button) view.findViewById(R.id.byte_btn_byte2str);
        btn_s2b = (Button) view.findViewById(R.id.byte_btn_str2byte);
        btn_b2s.setOnClickListener(this);
        btn_s2b.setOnClickListener(this);
    }

    private void initEvents() {
        mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {
                Log.i(TAG, "onDrawerStateChanged: ");
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.i(TAG, "onDrawerSlide: ");
                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1 - slideOffset;
                float rightScale = 0.8f + scale * 0.2f;

                if (drawerView.getTag().equals("LEFT")) {

                    float leftScale = 1 - 0.3f * scale;

                    ViewHelper.setScaleX(mMenu, leftScale);
                    ViewHelper.setScaleY(mMenu, leftScale);
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(mContent, mMenu.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(mContent, 0);
                    ViewHelper.setPivotY(mContent, mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                } else {
                    ViewHelper.setTranslationX(mContent, -mMenu.getMeasuredWidth() * slideOffset);
                    ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
                    ViewHelper.setPivotY(mContent, mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                }

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.i(TAG, "onDrawerOpened: ");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.i(TAG, "onDrawerClosed: ");
            }
        });
    }

    private void initPresenter() {
        bytePresenter = new BytePresenter(this);
    }

    //点击按钮，弹出侧滑菜单
    public void openLeftMenu() {
        mDrawerLayout.openDrawer(Gravity.LEFT);
    }

    public void openRightMenu() {
        mDrawerLayout.openDrawer(Gravity.RIGHT);
    }

    private void initData() {
        list.clear();
        for (int i = 0; i < strData.length; i++) {
            list.add(strData[i]);
        }
    }

    /*
    监听
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.byte_btn_byte2str:
                String str = bytePresenter.byte2str(et_raw.getText().toString().getBytes());
                bytePresenter.setConvertText(str);
                break;
            case R.id.byte_btn_str2byte:
                byte[] bytes = bytePresenter.str2byte(et_raw.getText().toString());
                bytePresenter.setConvertText(Arrays.toString(bytes));
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                flipper.setDisplayedChild(0);
                break;
            case 1:
                flipper.setDisplayedChild(1);
                break;
        }
        mDrawerLayout.closeDrawer(Gravity.LEFT);
    }

    /*
    接口实现
     */

    @Override
    public void setConvertText(String convertText) {
        tv_convert.setText(convertText);
    }
}
